package com.templatemethod.example;

import java.text.DecimalFormat;
import java.util.Random;

public abstract class Account {

	protected String accountNumber;

	public Account() {
		accountNumber = null;
	}

	public Account(String accountNumber) {
		System.out.println("当前账户：" + accountNumber);
		this.accountNumber = accountNumber;
	}

	/*
	 * 模板方法
	 */
	public final double calculateInterest() {
		// 账户类型对应的利息率
		double interestRate = doCalculateInterestRate();
		// 账户类型
		String accountType = doCalculateAccountType();
		// 账户当前金额
		double amount = calculateAmount(accountType, accountNumber);
		// 应付利息
		return amount * interestRate;
	}

	/*
	 * 基本方法
	 */
	protected abstract double doCalculateInterestRate();

	/*
	 * 基本方法
	 */
	protected abstract String doCalculateAccountType();

	/*
	 * 基本方法
	 */
	public final double calculateAmount(String accountType, String accountNumber) {
		double amount = Double.parseDouble(new DecimalFormat("#.00")
				.format(new Random().nextDouble() * 10000));
		System.out.println("当前账户金额：" + amount);
		return amount;
	}
}
